import {
  HttpApi
} from '../../model/index.js'
const httpApi = new HttpApi

let pageNum = 1;
let pageSize = 8;
let type;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dateStatusList: [{
      id: 1,
      name: '本周'
    }, {
      id: 2,
      name: '本月'
    }, {
      id: 3,
      name: '三个月'
    }],
    type: null,
    dateStatus: 1,
    list: [],
    pullUpOn: true, //没有更多了
  },
  /**
   * 去详情页面
   */
  onGoInfo: function(e) {
    let index = e.currentTarget.dataset.index;
    wx.setStorageSync('inspectionInfo', this.data.list[index])
    switch (type) {
      case "1":
        wx.navigateTo({
          url: '/pages/info-inspection/info-inspection'
        })
        break;
      case "2":
        wx.navigateTo({
          url: '/pages/info-maintenance/info-maintenance'
        })
        break;
    }
  },
  /**
   * 切换时间
   */
  onDateStatus: function(e) {
    let id = e.currentTarget.dataset.id;
    if (id == this.data.dateStatus) return;
    pageNum = 1;
    this.setData({
      dateStatus: id,
      pullUpOn: true,
      list: []
    })
    this.patrolRecordList()
  },
  /**
   * 获取记录
   */
  patrolRecordList: function() {
    let params = {
      dateStatus: this.data.dateStatus,
      pageNum: pageNum,
      pageSize: pageSize
    }
    wx.showLoading({
      title: '加载中',
    })
    switch (type) {
      case "1":
        httpApi.PatrolRecordList(params).then(res => {
          wx.hideLoading();
          this.data.list.push(...res.result.list);
          this.setData({
            list: this.data.list
          })
          if (res.result.list.length < pageSize){
            this.setData({
              pullUpOn: false
            })
          }
          wx.stopPullDownRefresh();
        })
        break;
      case "2":
        httpApi.MaintenanceRecordList(params).then(res => {
          wx.hideLoading();
          this.data.list.push(...res.result.list);
          this.setData({
            list: this.data.list
          })
          if (res.result.list.length < pageSize) {
            this.setData({
              pullUpOn: false
            })
          }
          wx.stopPullDownRefresh();
        })
        break;
    }
  },
  //下拉刷新 "enablePullDownRefresh": true
  onPullDownRefresh: function() {
    pageNum = 1;
    this.setData({
      list: []
    })
    this.patrolRecordList()
  },
  //上拉加载
  onReachBottom: function() {
    if (this.data.list.length < pageSize * pageNum) {
      this.setData({
        pullUpOn: false
      })
      return
    } else {
      pageNum++
      this.patrolRecordList()
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.type) {
      type = options.type
      this.setData({
        type: type
      })
      switch (type) {
        case "1":
          wx.setNavigationBarTitle({
            title: '巡检记录'
          })
          break;
        case "2":
          wx.setNavigationBarTitle({
            title: '维保记录'
          })
          break;
      }
      this.patrolRecordList()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    pageNum = 1;
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})